/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Unni Vemanchery Mana
 */
public class Util {
    
    public static void sliceImage(String img){
        
        
        File file = new File(img);
     //   if(file.exists()){
            FileInputStream fis;  
            try {
                //fis = new FileInputStream();
                BufferedImage image = ImageIO.read(Util.class.getClassLoader().getResourceAsStream(img)); //reading the image file 
                int rows = 2; //You should decide the values for rows and cols variables  
        int cols = 2;  
        int chunks = rows * cols;  
  
        int chunkWidth = image.getWidth() / cols; // determines the chunk width and height  
        int chunkHeight = image.getHeight() / rows;  
        int count = 0;  
        BufferedImage imgs[] = new BufferedImage[chunks]; //Image array to hold image chunks  
        for (int x = 0; x < rows; x++) {  
            for (int y = 0; y < cols; y++) {  
                //Initialize the image array with image chunks  
                imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());  
  
                // draws the image chunk  
                Graphics2D gr = imgs[count++].createGraphics();  
                gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);  
                gr.dispose();  
                
            }  
        }  
        System.out.println("Splitting done"); 
         //writing mini images into image files  
        for (int i = 0; i < imgs.length; i++) {  
            ImageIO.write(imgs[i], "png", new File("ship" + i + ".png"));  
        }  
        System.out.println("Mini images created");
            } catch (IOException ex) {
                ex.printStackTrace();
                Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
            } 
            
       // }
      //  else System.out.println(" no image file found.....");
    }
    
    public static void main(String []arg){
        sliceImage("player.png");
    }
}
